package com.shxzz.easy12306.services.ticket.service.handler.filter.purchase;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.shxzz.easy12306.framework.starter.convention.exceptions.ClientException;
import com.shxzz.easy12306.services.ticket.dto.domain.PurchaseTicketPassengerDetailDTO;
import com.shxzz.easy12306.services.ticket.dto.req.PurchaseTicketReqDTO;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 车票购买参数不为空校验
 * @author shxzz
 * @create 2024-03-06-13:59
 */
@Component
public class TicketPurchaseParamNotNullChainFilter implements TicketPurchaseChainFilter {

    @Override
    public void handler(PurchaseTicketReqDTO requestParam) {
        if (CharSequenceUtil.isBlank(requestParam.getTrainId())) {
            throw new ClientException("列车ID不能为空");
        }
        if (CharSequenceUtil.isBlank(requestParam.getDeparture())) {
            throw new ClientException("出发站不能为空");
        }
        if (CharSequenceUtil.isBlank(requestParam.getArrival())) {
            throw new ClientException("到达站不能为空");
        }
        if(CollUtil.isEmpty(requestParam.getPassengers())) {
            throw new ClientException("乘车人不能为空");
        }
        for (PurchaseTicketPassengerDetailDTO each : requestParam.getPassengers()) {
            if (CharSequenceUtil.isBlank(each.getPassengerId())) {
                throw new ClientException("乘车人ID不能为空");
            }
            if (Objects.isNull(each.getSeatType())) {
                throw new ClientException("座位类型不能为空");
            }
        }

    }

    @Override
    public int getOrder() {
        return 0;
    }
}
